<template>
    <div>

        <div style="margin: 10px 0">
            <el-input style="width: 200px" placeholder="请输入公司" suffix-icon="el-icon-search"
                      v-model="company"></el-input>
            <el-input class="ml-5" style="width: 200px" placeholder="请输入订单状态" suffix-icon="el-icon-search"
                      v-model="status"></el-input>
            <!--<el-input style="width: 200px" placeholder="请输入邮箱" suffix-icon="el-icon-search" class="ml-5" v-model="email"></el-input>-->
            <!--<el-input style="width: 200px" placeholder="请输入地址" suffix-icon="el-icon-search" class="ml-5" v-model="address"></el-input>-->
            <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
            <el-button type="warning" @click="reset">重置</el-button>
        </div>

        <div style="margin: 10px 0">
            <el-button type="primary" @click="handleAdd">新增 <i class="el-icon-circle-plus-outline"></i></el-button>
            <template>
                <el-popconfirm
                        class="ml-5"
                        confirm-button-text='确定'
                        cancel-button-text='我再想想'
                        icon="el-icon-info"
                        icon-color="red"
                        title="确定删除吗？"
                        @confirm="delBatch"
                >
                    <el-button type="danger" slot="reference">批量删除 <i class="el-icon-remove-outline"></i></el-button>
                </el-popconfirm>
            </template>
        </div>

        <el-table :data="tableData" border stripe :header-cell-class-name="'headerBg'"
                  @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column prop="id" label="ID" width="40"></el-table-column>
            <el-table-column prop="type" label="类型" width="50"></el-table-column>
            <el-table-column prop="email" label="邮箱" width="100"></el-table-column>
            <el-table-column prop="contactPerson" label="联系人" width="70"></el-table-column>
            <el-table-column prop="phone" label="电话" width="100"></el-table-column>
            <el-table-column prop="company" label="公司" width="100"></el-table-column>
            <el-table-column prop="destination" label="目的地" width="70"></el-table-column>
            <el-table-column prop="tamount" label="卡车数量" width="50"></el-table-column>
            <el-table-column prop="tdetail" label="卡车备注" width="50"></el-table-column>
            <el-table-column prop="damount" label="卸箱数" width="70"></el-table-column>
            <el-table-column prop="pamount" label="装箱数" width="70"></el-table-column>
            <el-table-column prop="updateTime" label="更新时间" width="90"></el-table-column>
            <el-table-column prop="arrangeTime" label="到达时间" width="90"></el-table-column>
            <el-table-column prop="finishTime" label="完成时间" width="90"></el-table-column>
            <el-table-column prop="status" label="状态" width="50"></el-table-column>
            <el-table-column prop="normal" label="普通箱" width="50"></el-table-column>
            <el-table-column prop="empty" label="空箱" width="50"></el-table-column>
            <el-table-column prop="heavy" label="重箱" width="50"></el-table-column>
            <el-table-column prop="freeze" label="冷冻箱" width="50"></el-table-column>
            <el-table-column prop="danger" label="危险箱" width="50"></el-table-column>
            <el-table-column prop="overweight" label="超重箱" width="50"></el-table-column>
            <el-table-column label="操作" width="200" align="center">
                <template slot-scope="scope">
                    <el-button type="success" @click="handleEdit(scope.row)">编辑 <i class="el-icon-edit"></i></el-button>
                    <template>
                        <el-popconfirm
                                class="ml-5"
                                confirm-button-text='确定'
                                cancel-button-text='我再想想'
                                icon="el-icon-info"
                                icon-color="red"
                                title="确定删除吗？"
                                @confirm="del(scope.row.id)"
                        >
                            <el-button type="danger" slot="reference">删除 <i class="el-icon-remove-outline"></i>
                            </el-button>
                        </el-popconfirm>
                    </template>
                </template>
            </el-table-column>
        </el-table>
        <div style="padding: 10px 0">
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="pageNum"
                    :page-sizes="[5, 10, 20, 50]"
                    :page-size="5"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
        </div>
        <el-dialog title="订单信息" :visible.sync="dialogFormVisible" width="30%">
            <el-form label-width="80px">
                <el-form-item label="订单状态" @change="jyf">
                    <el-radio v-model="form.type" label="0" :disabled="form.id">进箱</el-radio>
                    <el-radio v-model="form.type" label="1" :disabled="form.id">提箱</el-radio>
                    <el-radio v-model="form.type" label="2" :disabled="form.id">混合</el-radio>
                </el-form-item>
                <el-form-item label="联系人">
                    <el-input v-model="form.contactPerson" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="邮箱">
                    <el-input v-model="form.email" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="电话">
                    <el-input v-model="form.phone" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="公司">
                    <el-input v-model="form.company" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="目的地">
                    <el-radio v-model="form.destination" label="亚洲">亚洲</el-radio>
                    <el-radio v-model="form.destination" label="美洲">美洲</el-radio>
                    <el-radio v-model="form.destination" label="欧洲">欧洲</el-radio>
                </el-form-item>
                <el-form-item label="卡车数量">
                    <el-input v-model="form.tamount" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="卡车备注">
                    <el-input v-model="form.tdetail" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="普通箱">
                    <template>
                        <el-input-number v-model="form.normal" :min="0" :max="10" label="描述文字"
                                         :disabled="this.form.type == 1"></el-input-number>
                    </template>
                </el-form-item>
                <el-form-item label="空箱">
                    <template>
                        <el-input-number v-model="form.empty" :min="0" :max="10" label="描述文字"
                                         :disabled="this.form.type == 1"></el-input-number>
                    </template>
                </el-form-item>
                <el-form-item label="重箱">
                    <template>
                        <el-input-number v-model="form.heavy" :min="0" :max="10" label="描述文字"
                                         :disabled="this.form.type == 1"></el-input-number>
                    </template>
                </el-form-item>
                <el-form-item label="冷冻箱">
                    <template>
                        <el-input-number v-model="form.freeze" :min="0" :max="10" label="描述文字"
                                         :disabled="this.form.type == 1"></el-input-number>
                    </template>
                </el-form-item>
                <el-form-item label="危险箱">
                    <template>
                        <el-input-number v-model="form.danger" :min="0" :max="10" label="描述文字"
                                         :disabled="this.form.type == 1"></el-input-number>
                    </template>
                </el-form-item>
                <el-form-item label="超重箱">
                    <template>
                        <el-input-number v-model="form.overweight" :min="0" :max="10"
                                         label="描述文字" :disabled="this.form.type == 1"></el-input-number>
                    </template>
                </el-form-item>
                <el-form-item label="提箱">
                    <template>
                        <el-input-number v-model="form.pamount" :min="0" :max="20" label="描述文字"
                                         :disabled="this.form.type == 0"></el-input-number>
                    </template>
                </el-form-item>
                <el-form-item label="到达时间">
                    <template>
                        <el-date-picker
                                v-model="form.arrangeTime"
                                type="datetime"
                                placeholder="选择日期时间">
                        </el-date-picker>
                    </template>
                </el-form-item>
                <!--<el-form-item label="到达日期">-->
                <!--<div>-->
                <!--<el-date-picker-->
                <!--v-model="form.value1"-->
                <!--type="date"-->
                <!--placeholder="留白即默认使用明天当前时间">-->
                <!--</el-date-picker>-->
                <!--</div>-->
                <!--</el-form-item>-->
                <!--<el-form-item label="到达时间">-->
                <!--<el-time-select-->
                <!--placeholder="时间窗为起始时间开始1小时"-->
                <!--v-model="form.startTime"-->
                <!--:picker-options="{-->
                <!--start: '00:00',-->
                <!--step: '01:00',-->
                <!--end: '23:00',-->
                <!--}">-->
                <!--</el-time-select>-->
                <!--</el-form-item>-->
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取消</el-button>
                <el-button type="primary" @click="save">确定</el-button>
            </div>
        </el-dialog>
    </div>
</template>


<script>
    import {serverIp, serverPort} from "../config/config";

    export default {
        name: 'Home',
        data() {
            return {
                serverIp: serverIp,
                serverPort: serverPort,
                tableData: [],
                total: 0,
                pageNum: 1,
                pageSize: 5,
                company: "",
                form: {
                    arrangeTime: null
                },
                dialogFormVisible: false,
                multipleSelection: [],
                type: 3,
                status: "",
                dynamicValidateForm: {
                    domains: [{
                        value: ''
                    }],
                },
                email: "",
                userId: 0,
            }
        },
        created() {
            this.load()
        },
        methods: {
            load() {
                this.request.get("/user/order/page", {
                    params: {
                        pageNum: this.pageNum,
                        pageSize: this.pageSize,
                        company: this.company,
                        status: this.status,
                        username: JSON.parse(localStorage.getItem("user")).username
                    }
                }).then(res => {
                    this.tableData = res.data.data
                    this.total = res.data.total
                })
                this.request.get("/user/username", {
                    params: {
                        username: JSON.parse(localStorage.getItem("user")).username
                    }
                }).then(res => {
                    this.userId = res.data.id
                    this.email = res.data.email
                    this.phone = res.data.phone
                    this.contactPerson = res.data.contactPerson
                })
            },
            reset() {
                this.company = ""
                this.load()
            },
            handleSizeChange(pageSize) {
                this.pageSize = pageSize;
                this.load()
            },
            handleCurrentChange(pageNum) {
                this.pageNum = pageNum;
                this.load()
            },
            handleSelectionChange(val) {
                this.multipleSelection = val
            },
            handleAdd() {
                this.form = {
                    email: this.email,
                    phone: this.phone,
                    contactPerson: this.contactPerson,
                    userId: this.userId,
                    normal: 0,
                    empty: 0,
                    heavy: 0,
                    freeze: 0,
                    danger: 0,
                    overweight: 0,
                    pamount: 0
                }
                this.dialogFormVisible = true
            },
            save() {
                if (this.form.arrangeTime && this.form.arrangeTime.toString().length > 20) {
                    // console.log(this.form.arrangeTime)
                    this.form.arrangeTime = this.form.arrangeTime.Format('yyyy-MM-dd hh:mm:ss')
                }
                this.request.post("/user/order", this.form).then(res => {
                    if (res.code === '200') {
                        this.$message.success("保存成功")
                        this.dialogFormVisible = false
                        this.load()
                    } else {
                        this.$message.error("保存失败")
                    }
                })
            },
            handleEdit(row) {
                this.form = row
                this.dialogFormVisible = true
            },
            del(id) {
                this.request.get("/user/order/delete?id=" + id).then(res => {
                    if (res.data) {
                        this.$message.success("删除成功")
                        this.load()
                    } else {
                        this.$message.error("删除失败")
                    }
                })
            },
            delBatch() {
                let ids = this.multipleSelection.map(v => v.id)
                this.request.post("/user/order/delete/batch", ids).then(res => {
                    if (res.data) {
                        this.$message.success("删除成功")
                        this.load()
                    } else {
                        this.$message.error("删除失败")
                    }
                })
            },
        }
    }
</script>

<style>
    .headerBg {
        background: #eee !important;
    }
</style>
